package pl.omega.selenium;

import java.util.List;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import pl.omega.selenium.model.Film;

public class SearchGood extends pl.omega.selenium.pages.TestBase {
/*  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Test
  public void testUntitled() throws Exception {
	WebDriverWait wait = new WebDriverWait(driver, 30);
    driver.get(baseUrl + "/php4dvd/#!/sort/name%20asc/");
    WebElement loading = driver.findElement(By.id("loading"));
    driver.findElement(By.id("q")).clear();
    driver.findElement(By.id("q")).sendKeys("seven"+Keys.RETURN);
    wait.until(ExpectedConditions.visibilityOf(loading));
    //wait.until(ExpectedConditions.refreshed(ExpectedConditions.visibilityOf(loading)));
    List<WebElement> filmlist = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("movie_box")));
    assertNotEquals(0, filmlist.size());
   
   
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }*/
	
	@BeforeMethod
	public void mayBeLogout() {
		if (app.getUserHelper().isLoggedIn()) {
			return;
		}
		app.getUserHelper().loginAs(ADMIN);
	}
	
	@Test
	public void searchGood() {
		List<Film> films = app.getFilmHelper().search("seven");
	    assertNotEquals(0, films.size());
	}
}
